<template>
  <div class="box">
    <header class="header">
      <van-nav-bar title="修改用户信息"
                   left-arrow
                   @click-left="$router.back()" />
    </header>
    <div class="content">
      <van-form @submit="onSubmit">
        <van-field v-model="username"
                   name="用户名"
                   label="用户名"
                   placeholder="用户名"
                   :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field v-model="tel"
                   type="text"
                   name="手机号"
                   label="手机号"
                   placeholder="手机号"
                   :rules="[{ required: true, message: '请填写手机号' }]" />
        <van-field readonly
                   clickable
                   name="datetimePicker"
                   :value="value"
                   label="时间选择"
                   placeholder="点击选择时间"
                   @click="showPicker = true" />
        <van-popup v-model="showPicker"
                   position="bottom">
          <van-datetime-picker @confirm="onConfirm"
                               type="date"
                               v-model="currentDate"
                               title="选择年月日"
                               :min-date="minDate"
                               :max-date="maxDate" />
        </van-popup>
        <div style="margin: 16px;">
          <van-button round
                      block
                      type="info"
                      native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
      <van-button round
                  block
                  type="default"
                  native-type="submit"
                  @click="exit">
        退出
      </van-button>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, Form, Field, Button, DatetimePicker, Popup, Dialog } from 'vant'
import { getUserMsg } from '../../api/index.js'
import { mapMutations } from 'vuex'

Vue.use(NavBar)
Vue.use(Form)
Vue.use(Field)
Vue.use(Button)
Vue.use(DatetimePicker)
Vue.use(Popup)

export default {
  data () {
    return {
      username: '',
      tel: '',
      minDate: new Date(2018, 1, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      showPicker: false,
      value: ''
    }
  },
  mounted () {
    getUserMsg({
      userId: localStorage.getItem('userId')
    }).then(res => {
      // console.log(res)
    })
  },
  methods: {
    onSubmit () { },
    onConfirm (val) { 
      this.value = val.toLocaleDateString()
      this.showPicker = false
    },
    exit () {
      Dialog.confirm({
        message: '确认退出?',
        theme: 'round-button'
      }).then(() => {
        localStorage.removeItem('userId')
        localStorage.removeItem('isLogin')
        localStorage.removeItem('token')
        this.change_isLogin('')
        this.change_token('')
        this.change_userId('')
        this.$router.push('/index')
      }).catch(() => {

      })
    },
    ...mapMutations({
      change_isLogin: 'user/change_isLogin',
      change_token: 'user/change_token',
      change_userId: 'user/change_userId'
    })
  }

}

</script>

<style lang="scss" scoped>
.box ::v-deep .exit{
   width: 80%;
   margin: 20px auto;
}
</style>
